<template>
	<view>
		<uni-card :is-shadow="false" margin="20rpx 20rpx" padding="0 0 20rpx 0" :border="false">
			<template v-slot:title>
				<uni-section class="decoration" title="您对商家/菜品满意吗?" type="line"></uni-section>
			</template>
			<uni-list :border="true">
				<uni-list-item :title="'店铺名称'+item.Commotidy[0].name"
					thumb="https://vkceyugu.cdn.bspapp.com/VKCEYUGU-dc-site/460d46d0-4fcc-11eb-8ff1-d5dcf8779628.png"
					thumb-size="lg">
				</uni-list-item>
				<view class="comment-box">
					<view class="dis-flex align-items-center" style="margin-bottom: 20rpx;">
						<text style="margin-right: 20rpx;">服务</text>
						<uni-rate v-model="submitData.serviceLevel" />
					</view>
					<view class="dis-flex align-items-center" style="margin-bottom: 30rpx;">
						<text style="margin-right: 20rpx;">味道</text>
						<uni-rate v-model="submitData.tasteLevel" />
					</view>
					<textarea name="" id="textarea-style" cols="30" rows="10" v-model="submitData.txt"
						placeholder="说说味道怎么样,给大家参考"></textarea>
					<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
						:maxCount="9"></u-upload>
				</view>
			</uni-list>
			<button type="primary" @click="submit" style="background-color: #ffd100;">提交</button>
		</uni-card>
	</view>
</template>

<script>
	import appInfo from "@/utils/appInfo.js"
	import {
		userCommentApi
	} from "@/api/index.js"
	export default {
		data() {
			return {
				submitData: {
					orders: null,
					business: null,
					serviceLevel: 0,
					tasteLevel: 0,
					txt: "",
				},
				fileList1: [],
			}
		},
		onLoad(options) {
			console.log(options);
			if (options.item) {

			}
		},
		methods: {
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: appInfo.upload_url,
						filePath: url,
						name: 'file',
						formData: {
							'openid': wx.getStorageSync('openid'),
							'X-W-Token': uni.getStorageSync('token'),
						},
						header: {
							'content-type': 'multipart/form-data;',
							'X-W-Token': uni.getStorageSync('token'),
						},
						success: (res) => {
							let data = JSON.parse(res.data);
							console.log(data);
							setTimeout(() => {
								resolve(data.data.path)
							}, 1000)
						}
					});
				})
			},
			// 提交评价
			async submit() {
				let submitData = this.submitData
				if (!submitData.txt) {
					uni.showToast({
						title: '请填写评价哦',
						icon: 'none'
					})
					return
				}
				submitData.images = this.fileList1.map(item => item.url).join(',')
				console.log(submitData);
				const {
					data: res
				} = await userCommentApi(submitData)
				console.log(res);
			}
		}
	}
</script>

<style scoped lang="less">
	.comment-box {
		padding: 20rpx;
		background-color: #f5f5f5;
		border-radius: 10rpx;
	}

	#textarea-style {
		// border: 1rpx solid #ffd100 !important;
	}

	/deep/ .u-upload__button {
		background-color: #f7f7f7 !important;
	}
</style>